import {createRouter, createWebHistory} from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/LoginView.vue'
import OrderCreateView from '../views/order/OrderCreateView.vue'
import OrderListView from '../views/order/OrderListView.vue'
import OrderDetailView from '../views/order/OrderDetailView.vue'
import WaybillUnassignedView from '../views/waybill/WaybillUnassignedView.vue'
import WaybillAssignedView from '../views/waybill/WaybillAssignedView.vue'
import DeliveryListView from '../views/DeliveryListView.vue'
import BillingReceivableView from '../views/bill-center/BillingReceivableView.vue'
import BillingReconciliationView from '../views/bill-center/BillingReconciliationView.vue'
import InvoiceManageView from '../views/bill-center/InvoiceManageView.vue'
import OrderStatistics from '../views/bill-center/OrderStatistics.vue'
import OrderConfigView from '../views/system-management/OrderConfigView.vue'
import TemplateConfigView from '../views/system-management/TemplateConfigView.vue'
import SmsConfigView from '../views/system-management/SmsConfigView.vue'
import PaymentConfigView from '../views/system-management/PaymentConfigView.vue'
import RoleManageView from '../views/permisson-manage/RoleManageView.vue'
import MenuManageView from '../views/permisson-manage/MenuManageView.vue'
import CompanyInfoView from '../views/enterprise-management/CompanyInfoView.vue'
import EmployeeManageView from '../views/enterprise-management/EmployeeManageView.vue'
import CustomerManageView from '../views/customer/CustomerManageView.vue'
import DriverManageView from '../views/transport/DriverManageView.vue'
import VehicleManageView from '../views/transport/VehicleManageView.vue'
import {authAPI} from '@/http/leancloud'
import StationManagementView from "@/views/station/StationManagementView.vue";

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/login',
            name: 'login',
            component: LoginView,
            meta: {
                requiresAuth: false,
                title: '登录 - TMS运输管理系统'
            }
        },
        {
            path: '/',
            name: 'home',
            component: HomeView,
            meta: {
                requiresAuth: true,
                title: '首页 - TMS运输管理系统'
            }
        },
        {
            path: '/order/create',
            name: 'order-create',
            component: OrderCreateView,
            meta: {
                requiresAuth: true,
                title: '开单 - TMS运输管理系统'
            }
        },
        {
            path: '/orders',
            name: 'order-list',
            component: OrderListView,
            meta: {
                requiresAuth: true,
                title: '订单列表 - TMS运输管理系统'
            }
        },
        {
            path: '/order/:id',
            name: 'order-detail',
            component: OrderDetailView,
            meta: {
                requiresAuth: true,
                title: '订单详情 - TMS运输管理系统'
            }
        },
        {
            path: '/waybill/unassigned',
            name: 'waybill-unassigned',
            component: WaybillUnassignedView,
            meta: {
                requiresAuth: true,
                title: '待配载运单 - TMS运输管理系统'
            }
        },
        {
            path: '/waybill/assigned',
            name: 'waybill-assigned',
            component: WaybillAssignedView,
            meta: {
                requiresAuth: true,
                title: '已配载运单 - TMS运输管理系统'
            }
        },
        {
            path: '/delivery',
            name: 'delivery-list',
            component: DeliveryListView,
            meta: {
                requiresAuth: true,
                title: '配送管理 - TMS运输管理系统'
            }
        },
        {
            path: '/billing/receivable',
            name: 'billing-receivable',
            component: BillingReceivableView,
            meta: {
                requiresAuth: true,
                title: '应收单管理 - TMS运输管理系统'
            }
        },
        {
            path: '/billing/reconciliation',
            name: 'billing-reconciliation',
            component: BillingReconciliationView,
            meta: {
                requiresAuth: true,
                title: '应收对账单 - TMS运输管理系统'
            }
        },
        {
            path: '/billing/invoice',
            name: 'billing-invoice',
            component: InvoiceManageView,
            meta: {
                requiresAuth: true,
                title: '发票管理 - TMS运输管理系统'
            }
        },
        {
            path: '/billing/statistics',
            name: 'billing-statistics',
            component: OrderStatistics,
            meta: {
                requiresAuth: true,
                title: '订单统计 - TMS运输管理系统'
            }
        },
        {
            path: '/system/order-config',
            name: 'order-config',
            component: OrderConfigView,
            meta: {
                requiresAuth: true,
                title: '开单配置 - TMS运输管理系统'
            }
        },
        {
            path: '/system/template-config',
            name: 'template-config',
            component: TemplateConfigView,
            meta: {
                requiresAuth: true,
                title: '模板配置 - TMS运输管理系统'
            }
        },
        {
            path: '/system/role-manage',
            name: 'role-manage',
            component: RoleManageView,
            meta: {
                requiresAuth: true,
                title: '角色管理 - TMS运输管理系统'
            }
        },
        {
            path: '/system/menu-manage',
            name: 'menu-manage',
            component: MenuManageView,
            meta: {
                requiresAuth: true,
                title: '菜单管理 - TMS运输管理系统'
            }
        },
        {
            path: '/system/sms-config',
            name: 'sms-config',
            component: SmsConfigView,
            meta: {
                requiresAuth: true,
                title: '短信配置 - TMS运输管理系统'
            }
        },
        {
            path: '/system/payment-config',
            name: 'payment-config',
            component: PaymentConfigView,
            meta: {
                requiresAuth: true,
                title: '支付配置 - TMS运输管理系统'
            }
        },
        {
            path: '/company/info',
            name: 'company-info',
            component: CompanyInfoView,
            meta: {
                requiresAuth: true,
                title: '公司信息 - TMS运输管理系统'
            }
        },
        {
            path: '/company/employee',
            name: 'employee-manage',
            component: EmployeeManageView,
            meta: {
                requiresAuth: true,
                title: '员工管理 - TMS运输管理系统'
            }
        },
        {
            path: '/station/manage',
            name: 'station-manage',
            component: StationManagementView,
            meta: {
                requiresAuth: true,
                title: '站点管理 - TMS运输管理系统'
            }
        },
        {
            path: '/customers',
            name: 'customer-manage',
            component: CustomerManageView,
            meta: {
                requiresAuth: true,
                title: '客户管理 - TMS运输管理系统'
            }
        },
        {
            path: '/carriers/drivers',
            name: 'driver-manage',
            component: DriverManageView,
            meta: {
                requiresAuth: true,
                title: '司机管理 - TMS运输管理系统'
            }
        },
        {
            path: '/carriers/vehicles',
            name: 'vehicle-manage',
            component: VehicleManageView,
            meta: {
                requiresAuth: true,
                title: '车辆管理 - TMS运输管理系统'
            }
        },
        {
            path: '/about',
            name: 'about',
            // 使用懒加载方式引入组件，提高首屏加载速度
            component: () => import('../views/AboutView.vue'),
            meta: {
                requiresAuth: true,
                title: '关于 - TMS运输管理系统'
            }
        }
    ]
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
    // 设置页面标题
    document.title = to.meta.title as string || 'TMS运输管理系统'

    // 判断页面是否需要登录权限
    if (to.meta.requiresAuth) {
        // 获取当前用户信息
        const currentUser = authAPI.getCurrentUser()

        if (!currentUser) {
            // 用户未登录，重定向到登录页面
            next({
                path: '/login',
                query: {redirect: to.fullPath} // 将原本要访问的页面路径作为参数传递
            })
        } else {
            // 用户已登录，允许访问
            next()
        }
    } else {
        // 不需要登录权限的页面直接放行
        next()
    }
})

export default router
